package com.atguigu.xml;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Date:2020/9/4
 * Author:ybc
 * Description:
 */
public class CreateXML {

    public static void main(String[] args) throws IOException {

        //创建一个Document对象
        Document document = DocumentHelper.createDocument();
        //创建并设置根标签
        Element studentElement = document.addElement("student");
        document.setRootElement(studentElement);
        //在根标签中添加描述学生信息的标签：name，sex，age
        Element nameElement = studentElement.addElement("name");
        Element sexElement = studentElement.addElement("sex");
        Element ageElement = studentElement.addElement("age");
        //为每个标签赋值
        nameElement.addText("张三");
        sexElement.addText("男");
        ageElement.addText("23");
        //输出xml的结构
        OutputFormat format = new OutputFormat("    ", true);
        OutputStream os = new FileOutputStream("student.xml");
        XMLWriter xmlWriter = new XMLWriter(os, format);
        xmlWriter.write(document);

    }

}
